package com.kermitye.treenodelib

import java.util.*

abstract class TreeNodeDelegate<T> {
    var adapter: TreeNodeAdapter<T>? = null

    // 多选个数 -->> 0：任意 非0：限制数量
    private var maxSelectCount = 0

    // 获取选中树节点集合
    var selectTreeNodeList: MutableList<TreeNode<T>> = ArrayList<TreeNode<T>>()

    /**
     * 层级tag，对应[com.kermitye.treenodelib.TreeNode.getLevel]
     */
    var levelTag: Int = setLevelTag()

    /**
     * 指定对应等级数，从0开始
     * 对应[com.kermitye.treenodelib.TreeNode.getLevel]
     */
    abstract fun setLevelTag(): Int

    /**
     * 是否是当前类型
     *
     * @param treeNode 树节点
     * @return 当前类型
     *
     * 使用levelTag进行判断
     */
//    abstract fun isItemType(treeNode: TreeNode<T>?): Boolean

    /**
     * 当前根视图布局id
     *
     * @return 布局id
     */
    abstract val layoutId: Int

    /**
     * 视图适配
     *
     * @param holder   视图持有者
     * @param treeNode 树节点
     */
    abstract fun convert(holder: BaseViewHolder, treeNode: TreeNode<T>)

    /**
     * 设置可选树节点最大数量
     *
     * @param maxSelectCount 数量
     */
    fun setMaxSelectCount(maxSelectCount: Int) {
        this.maxSelectCount = maxSelectCount
    }


    /**
     * 改变选中状态，并刷新
     * 当限制最多选中数量，超出的部分会把先加入的树节点移除
     *
     * @param treeNode 树节点
     * @param select   选中状态
     */
    fun selectTreeNode(treeNode: TreeNode<T>, select: Boolean) {
        treeNode.isSelect = select
        if (select) {
            if (!selectTreeNodeList.contains(treeNode)) { // 添加并刷新
                selectTreeNodeList.add(treeNode)
                adapter?.notifyTreeNode(treeNode)
            }
            if (maxSelectCount != 0) { // 多选且不限制数量
//                if (selectTreeNodeList.size > maxSelectCount) { // 删除首个已选并刷新
                //数量限制，针对对应父类下的数量，而不是层级下所有的数量
                val currentSelected = selectTreeNodeList.filter { it.parent == treeNode.parent }
                if (currentSelected.size > maxSelectCount) {
                    val first: TreeNode<T> = currentSelected[0]
                    selectTreeNode(first, false)
//                    first.isSelect = false
//                    selectTreeNodeList.remove(first)
//                    adapter?.notifyTreeNode(first)
                }
            }
            //子类勾选时，父类选中
            if (treeNode.hasParent()) {
                adapter?.getDelegateByLevel(treeNode.parent!!.getLevel())
                    ?.selectTreeNode(treeNode.parent!!, select)
            }
        } else {
            if (selectTreeNodeList.contains(treeNode)) { // 移除并刷新
                if (treeNode.hasChild()) {
                    treeNode.children?.forEach {
                        adapter?.getDelegateByLevel(it.getLevel())
                            ?.selectTreeNode(it, select)
                    }
                }
                selectTreeNodeList.remove(treeNode)
                adapter?.notifyTreeNode(treeNode)

            }
        }
    }



    /**
     * 改变树节点选中状态
     *
     * @param treeNode 树节点
     */
    fun changeSelectTreeNode(treeNode: TreeNode<T>) {
        selectTreeNode(treeNode, !treeNode.isSelect)
    }


}